<?php

namespace common\behavior;

use Webman\Stomp\Client;

/**
 * 新增实体,自动发布清单
 */
class EntityAutoPublishBehavior
{

    /**
     * 自动发布清单
     * @param $params
     * @return void
     * @throws \think\Exception
     */
    public function run(&$params)
    {
        if (empty($params['pk']) || empty($params['data']['module_id']) || empty($params['data']['tenant_id'])) {
            return;
        }

        $entityId = $params['pk'];
        $moduleId = $params['data']['module_id'];
        $tenantId = $params['data']['tenant_id'];
        $assetShotEntityEntity = [
            module_by_code('design')['id'], // 概念
            module_by_code('storyboard')['id'], // 分镜
        ];
        if (!in_array($moduleId, $assetShotEntityEntity)) {
            return;
        }

        // 手动确认
        if (!empty($params['data']['is_confirm']) && $params['data']['is_confirm'] == 'yes') {
            return;
        }

        $buildQueueDataWithXu = build_queue_data_with_xu(
            ['filter' => ['id' => $entityId], 'module_code' => module_by_id($moduleId)['code'], 'tenant_id' => $tenantId, 'user_id' => request()->getUserId()]
        );
        Client::send('entity_publish', $buildQueueDataWithXu);
    }
}
